package gms.actions.match;

import gms.actions.SuperAction;
import gms.entities.match.MatchInfo;
import gms.service.match.MatchInfoService;
import gms.util.PageBean;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

public class MatchInfoAction extends SuperAction implements ModelDriven<MatchInfo> {

	private MatchInfo matchInfo = new MatchInfo();  //赛事信息表模型
	
	@Override
	public MatchInfo getModel() {		
		return matchInfo;
	}
	
	public MatchInfo getMatchInfo() {
		return matchInfo;
	}

	public void setMatchInfo(MatchInfo matchInfo) {
		this.matchInfo = matchInfo;
	}
	
	private int currPage;  //当前页面显示的页数
	
	public int getCurrPage() {
		return currPage;
	}

	public void setCurrPage(int currPage) {
		this.currPage = currPage;
	}
	
	private String idList; //赛事申请表id集合
	
	public String getIdList() {
		return idList;
	}

	public void setIdList(String idList) {
		this.idList = idList;
	}
	
	private MatchInfoService matchInfoService;  //赛事信息表服务接口
	
	public void setMatchInfoService(MatchInfoService matchInfoService) {
		this.matchInfoService = matchInfoService;
	}
	
	/**
	 * 通过时间模糊查询赛事信息表
	 * @return
	 * @throws ParseException 
	 */
	public String queryByTime() throws ParseException{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date startTime = sdf.parse("2017-5-22");
		Date endTime = sdf.parse("2017-5-24");
		matchInfo.setStartTime(startTime);
		matchInfo.setEndTime(endTime);
		System.out.println(matchInfo.toString());
		return NONE;
	}
	
	/**
	 * 通过属性精确查找赛事信息表
	 * @return
	 * @throws ParseException 
	 */
	public String queryByAttr() throws ParseException{
		System.out.println(matchInfo.toString());
		matchInfoService.queryByAttr(matchInfo);
		return NONE;
	}
	
	/**
	 * 分页查询当前页数所有记录，返回分页查询结果界面
	 * @return 
	 */
	public String findAll(){
		PageBean<MatchInfo> pageBean = matchInfoService.findByPage(currPage);
		ActionContext.getContext().getValueStack().push(pageBean);
		return "findAll";
	}
	
	/**
	 * 列出所有赛事信息表，返回更新列表界面
	 * @return
	 */
	public String listUpdate(){
		PageBean<MatchInfo> pageBean = matchInfoService.findByPage(currPage);
		ActionContext.getContext().getValueStack().push(pageBean);
		return "listUpdate";
	}
	
	/**
	 * 更新赛事信息表，成功返回成功界面，失败返回失败信息
	 * @return
	 */
	public String update(){
		matchInfoService.update(matchInfo);
		return "updateSuccess";
	}
	
	/**
	 * 删除赛事信息表，返回删除成功界面
	 * @return
	 */
	public String delete(){
		PageBean<MatchInfo> pageBean = matchInfoService.findByPage(currPage);
		ActionContext.getContext().getValueStack().push(pageBean);
		return "delete";
	}
	
	/**
	 * 批量删除选中的赛事信息表，删除成功返回原界面
	 * @return
	 */
	public String deleteAll(){
		System.out.println(idList);
		String[] l = idList.split(",");
		for (String id : l) {
			matchInfo = matchInfoService.findByID(Integer.parseInt(id));
			matchInfoService.delete(matchInfo);
		}
		System.out.println("deleteAll success");
		return "deleteSuccess";
	}
}
